package com.netease.videoLoaderDemo;


import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.netease.neliveplayer.playerkit.sdk.view.AdvanceTextureView;
import com.netease.neliveplayer.playerkit.videoLoader.OnVideoItemScrollEventHandler;
import com.netease.neliveplayer.playerkit.videoLoader.VideoLoaderImpl;
import com.netease.videoLoaderDemo.service.VideoInfo;
import com.netease.videoSlidingKit.R;

import java.util.concurrent.TimeUnit;

public class LiveViewPagerFragment extends Fragment {
    private OnVideoItemScrollEventHandler handler;
    private int position;
    private AdvanceTextureView canvas;
    SeekBar mSeekBar;
    TextView playedTime;
    TextView totalTime;
    SpriteImageInfo spriteInfo;
    ImageView imageView;
    boolean needLoadSpriteImage = false;

    private int index = 1;

    public OnVideoItemScrollEventHandler getHandler() {
        return handler;
    }

    public void setHandler(OnVideoItemScrollEventHandler handler) {
        this.handler = handler;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public AdvanceTextureView getCanvas() {
        return canvas;
    }

    public void setSpriteInfo(SpriteImageInfo spriteInfo) {
        this.spriteInfo = spriteInfo;
    }

    public static LiveViewPagerFragment newInstance(VideoInfo info, OnVideoItemScrollEventHandler handler, int position, SpriteImageInfo spriteInfo) {
        LiveViewPagerFragment fragment = new LiveViewPagerFragment();
        Bundle args = new Bundle();
        args.putParcelable("VideoInfo", info);
        fragment.setArguments(args);
        fragment.setHandler(handler);
        fragment.setPosition(position);
        fragment.setSpriteInfo(spriteInfo);
        return fragment;
    }

    @SuppressLint("ClickableViewAccessibility")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.show_live_detail_fragment, container, false);
        canvas = rootView.findViewById(R.id.live_surface);
        mSeekBar = rootView.findViewById(R.id.seekBar);
        playedTime = rootView.findViewById(R.id.play_time);
        totalTime = rootView.findViewById(R.id.play_total_time);
        imageView = rootView.findViewById(R.id.imageView);
        imageView.setClipToOutline(true);
        mSeekBar.setOnTouchListener(new View.OnTouchListener() {
            ViewGroup.LayoutParams layoutParams = mSeekBar.getLayoutParams();
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        layoutParams =  mSeekBar.getLayoutParams();
                        mSeekBar.setLayoutParams(layoutParams);
                        mSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_progress));
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        layoutParams = mSeekBar.getLayoutParams();
                        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                        mSeekBar.setLayoutParams(layoutParams);
                        mSeekBar.setProgressDrawable(getResources().getDrawable(R.drawable.seekbar_progress_default));
                        break;
                }
                return false;
            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            private String mDuration;

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 这里可以根据滑动的进度执行相应操作
                String current = getFormatTime(progress * VideoLoaderImpl.getInstance().getDuration()/100);
                playedTime.setText(current);
                if(spriteInfo != null && needLoadSpriteImage) {
                    SpriteImageHelper.getInstance().downloadThumbs(getContext(),spriteInfo);
                    int thumbIndex = SpriteImageHelper.getInstance().getCurrentThumbIndex(spriteInfo, progress * VideoLoaderImpl.getInstance().getDuration() /100);
                    Bitmap bitmap = SpriteImageHelper.getInstance().getThumbBitmap(getContext(), spriteInfo, thumbIndex);
                    imageView.setImageBitmap(bitmap);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 当用户开始触摸滑动条时触发
                needLoadSpriteImage = true;
                OnVideoItemScrollEventHandler.needRefreshProgressBar = false;
                mDuration = getFormatTime(VideoLoaderImpl.getInstance().getDuration());
                totalTime.setText("/" + mDuration);
                totalTime.setVisibility(View.VISIBLE);
                playedTime.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.VISIBLE);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                needLoadSpriteImage = false;
                OnVideoItemScrollEventHandler.needRefreshProgressBar = true;
                VideoLoaderImpl.getInstance().seekTo(seekBar.getProgress());
                totalTime.setVisibility(View.GONE);
                playedTime.setVisibility(View.GONE);
                imageView.setVisibility(View.GONE);
            }

        });
        handler.onCreateView(canvas, mSeekBar);
        return rootView;
    }

    public void stopFragment() {
    }
    @SuppressLint("DefaultLocale")
    private String getFormatTime(long milliseconds){
        String hms = String.format("%02d:%02d:%02d",
                TimeUnit.MILLISECONDS.toHours(milliseconds),
                TimeUnit.MILLISECONDS.toMinutes(milliseconds) % TimeUnit.HOURS.toMinutes(1),
                TimeUnit.MILLISECONDS.toSeconds(milliseconds) % TimeUnit.MINUTES.toSeconds(1));

        if (TimeUnit.MILLISECONDS.toHours(milliseconds) == 0) {
            hms = String.format("%02d:%02d",
                    TimeUnit.MILLISECONDS.toMinutes(milliseconds),
                    TimeUnit.MILLISECONDS.toSeconds(milliseconds) % TimeUnit.MINUTES.toSeconds(1));
        }
        return hms;
    }
}
